package com.itwn.comic.web;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInfo;
import com.itwn.comic.entity.Report;
import com.itwn.comic.reult.ResponseResult;
import com.itwn.comic.service.ReportService;
import com.itwn.comic.vo.ReportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @author cx
 * @date 2022/2/19
 * @apiNote
 */
@RestController
@RequestMapping("report")
public class ReportController {
    @Value("${imgPath}")
    String imgPath;

    @Autowired
    ReportService reportService;

    @DeleteMapping("/del/{id}")
    public ResponseResult<Void> remove(@PathVariable("id") long id){
        ResponseResult<Void> result = new ResponseResult<>();
        try{
            this.reportService.removeByKey(id);
            result.setCode(200);
            result.setMsg("删除举报成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除举报失败!");
        }
        return result;
    }

    /**
     * 修改
     * @param report
     * @return
     */
    @PutMapping("/modify")
    public ResponseResult<Void> modify(@RequestBody Report report){

        ResponseResult<Void> result = new ResponseResult<>();
        try{
            this.reportService.modify(report);
            result.setCode(200);
            result.setMsg("编辑成功！");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("编辑失败！");
        }
        return result;
    }

    /**
     * 添加
     * @param report
     * @return
     */
    @PostMapping("/add")
    public ResponseResult<Void> add(@RequestBody Report report){
        //remark.setStatus("1"); 数据库给默认也可以
        report.setReportTime(new Date());
        report.setStatus(0);
        ResponseResult<Void> result = new ResponseResult<>();
        try{
            this.reportService.add(report);
            result.setCode(200);
            result.setMsg("保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("保存失败!");
        }
        return result;
    }


    /**
     * 分页查询举报的漫画
     * @param now
     * @param size
     * @return
     */
    @GetMapping("/listComic")
    public ResponseResult<PageInfo<Report>> listComicByPage(@RequestParam(value="now",defaultValue = "1") Integer now,
                                                       @RequestParam(value="size",defaultValue = "3") Integer size
                                                       ){
        ResponseResult<PageInfo<Report>> result = new ResponseResult<>();
        try{
            PageInfo<Report> pageInfo = this.reportService.queryComicByPage(now,size);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }
    /**
     * 分页查询举报的评论
     * @param now
     * @param size
     * @return
     */
    @GetMapping("/listRemark")
    public ResponseResult<PageInfo<Report>> listRemarkByPage(@RequestParam(value="now",defaultValue = "1") Integer now,
                                                       @RequestParam(value="size",defaultValue = "3") Integer size
                                                       ){
        ResponseResult<PageInfo<Report>> result = new ResponseResult<>();
        try{
            PageInfo<Report> pageInfo = this.reportService.queryRemarkByPage(now,size);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }

    /**
     * 图片上传的方法(就这一个)
     * @param uploadFile
     * @return
     */
    @RequestMapping("/upload")
    public ResponseResult<String> upload(@RequestParam(value ="headerImg") MultipartFile uploadFile){
        System.out.println(uploadFile.getOriginalFilename());
        String filename = uploadFile.getOriginalFilename();
        // 重新获取名字，保证不重复
        String subFix = filename.substring(filename.lastIndexOf("."));
        filename = "comic" + System.currentTimeMillis()+subFix;
        // D:/imgs/comic2858292783742947.jpg
        File file = new File(imgPath + filename);

        ResponseResult<String> result = new ResponseResult<>();
        try {
            uploadFile.transferTo(file);
            result.setCode(200);
            result.setMsg("上传成功");
            result.setData(filename);
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("上传失败");
        }
        return result;
    }

    /**
     *
     * @param pageNum
     * @param pageSize
     * @param status
     * @param reportType
     * @param reportUserId
     * @param resourceId
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<PageInfo<ReportVo>> list(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "pageSize",defaultValue = "4") Integer pageSize,
                                                   @RequestParam(defaultValue = "-1") Integer status,
                                                   @RequestParam(defaultValue = "1") Integer reportType,
                                                   Integer reportUserId,
                                                   Integer resourceId){
        ResponseResult<PageInfo<ReportVo>> result = new ResponseResult<>();
        System.out.println(status);
        try {
            PageInfo<ReportVo> pageInfo=reportService.query(pageNum,pageSize,status,reportType,reportUserId,resourceId);
            result.setCode(200);
            result.setMsg("数据初始化成功");
            result.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("数据初始化失败");
        }
        return result;
    }

    /**
     *
     * @param ids
     * @param fromStatus
     * @param toStatus
     * @return
     */
    @RequestMapping("/updateStatus")
    public ResponseResult<Void> updateStatus(String ids ,Integer fromStatus,Integer toStatus){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            result.setCode(200);
            result.setMsg("更新成功");
            reportService.updateStatus(ids,fromStatus,toStatus);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("更新失败");
        }
        return result;
    }





}
